package com.augurit.common.util;

import javax.servlet.http.HttpServletRequest;

import com.augurit.common.base.form.ResultForm;
import com.augurit.common.util.lang.ArrayUtils;
import com.augurit.common.util.lang.StringUtils;

/**
 * 提供Action层的公共方法或工具方法
 */
public class ActionUtils {
	
	/**
	 * 选中记录ID集合标记
	 */
	public static final String CHECKED_IDS = "checkedIds";
	
	/**
	 * 选中记录ID集合标记
	 */
	public static final String CHECKED_IDS_SEPARATOR = ",";
	
	/**
	 * 获取所选中的记录ID集合
	 * @param request
	 * @return
	 */
	public static Long[] getCheckedIds(final HttpServletRequest request){
		String ids = request.getParameter(CHECKED_IDS);
		return ArrayUtils.convertToLongArray(
				StringUtils.split(ids, CHECKED_IDS_SEPARATOR));
	}
	
	/**
	 * 获取所选中的记录ID集合
	 * @param request
	 * @return
	 */
	public static Long[] getAllCheckedIds(final HttpServletRequest request){
		Long[] ids = null;
		String[] idsStr = request.getParameterValues(CHECKED_IDS);
		
		if(idsStr != null && idsStr.length > 0){
			ids = new Long[idsStr.length];
			for(int i = 0; i < idsStr.length; i++){
				ids[i] = Long.parseLong(idsStr[i]);
			}
		}
		return ids;
	}
	
	/**
	 * 设置错误的返回结果信息
	 * @param result
	 * @param errMsg
	 */
	public static void setErrMsgOfResult(ResultForm result, String errMsg){
		result.setSuccess(false);
		result.setErrMsg(errMsg);
	}
}
